-- OttoMateAction.applescript
-- Set Form Values of Current Webpage

--  Created by itod on 3/30/07.
--  Copyright 2007 Todd Ditchendorf. All rights reserved.

tell application "Safari"
	my waitForPageLoad(60)
	set retVal to (do JavaScript "%@" in document 1)
	set docLocation to (do JavaScript "document.location.href" in document 1)
	return retVal & " :" & docLocation
end tell

on waitForPageLoad(timeout_value) -- in seconds
	delay ("%@" as real)
	repeat with i from 1 to timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				delay 0.3
				if name of window 1 contains "Loading" then delay 1
				if name of window 1 contains "Loading" then delay 1
				if name of window 1 contains "Loading" then delay 1
				return true
			else if i is timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end waitForPageLoad